package com.ac.app.web.action.pick;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

import com.caac.core.Global;
import com.caac.core.config.OrderClazz;
import com.caac.core.config.QueryClass;
import com.caac.core.dao.CriteriaCallback;
import com.caac.core.dm.OrderProp;
import com.caac.core.util.Page;
import com.caac.core.web.action.BaseExportAction;
import com.caac.core.web.action.ViewForward;
import com.caac.core.web.form.BaseForm;
import com.ac.app.dm.PickRecord;
import com.ac.app.dm.PickUrl;
import com.ac.app.web.form.PkRecordSearchForm;
import com.ac.app.web.view.PkRecordView;


public class ExportPkBackRecordAction extends BaseExportAction {
	private static final String QUERY_NAME = "pkRecordQuery";

	private static final String ORDER_NAME = "pkRecordOrderBk";

	private static final String VIEW_NAME = "exportPkRecordView";

	protected ViewForward doViewExecute(ActionMapping mapping, BaseForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			PkRecordSearchForm myForm = (PkRecordSearchForm) form;
			QueryClass qc = getQuery(QUERY_NAME);
			
	        String whereStr="1=1 ";       
	        String key  = request.getParameter("key");
	        if(key!=null && key.length()>0 ){
	        	String[] keys = key.split(" ");
	        	for(int i=0;i<keys.length;i++){
	        	   whereStr += " and content like '%"+ keys[i]+"%'";
	        	}
	        	whereStr = "("+whereStr+")";
	        }
			CriteriaCallback cc = getCc(whereStr);
	        if(myForm.getOrderstr()==null || "".equals(myForm.getOrderstr())){
	        	myForm.setOrderFlag(1);
	        	myForm.setOrderstr("prjBegdate");
	        }
	        OrderClazz oc =  getOc(myForm.getOrderstr(),myForm.getOrderFlag());

	        List dmList = getBaseManager().getObjects(PickRecord.class, qc,
					myForm, myForm.isAnd(),oc, cc);
	
			List<PkRecordView> vList = new ArrayList<PkRecordView>();
			for (int i = 0; i < dmList.size(); i++) {
				PickRecord dm = (PickRecord) dmList.get(i);
				PkRecordView dmView = new PkRecordView();
				dmView.setPickRecord(dm);
				vList.add(dmView);
			}
			return new ViewForward(forward(mapping), vList,
					getSelViewProps(VIEW_NAME), null, null);
	}

	private static CriteriaCallback getCc(final String whereStr) {
		return new CriteriaCallback() {
			public void execute(Criteria criteria) {
				criteria.add(Expression.sql(whereStr));
			}
		};
	}
	
    private OrderClazz getOc(String orderStr,int orderFlag){
    	List list = new ArrayList();   
    	if(orderStr!=null && !orderStr.equals("")){
    		OrderProp op = new OrderProp();
    		op.setPropName(orderStr);
    		op.setGroupName("pkRecordOrderBk0");
    		if(orderFlag==1){
        		op.setAsc(false);
    		}else{
        		op.setAsc(true);
    		}
    		list.add(op);
    	}
    	return new OrderClazz(list);
    }
}
